from flask import Flask,request, jsonify
import pymysql
import pandas
from numpy import *
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
import json
from flask-cors import *
import os
app = Flask(__name__,static_folder='F:/PIC')
CORS(app, supports_credentials=True)#跨域连接
#推荐算法
@app.route('/search',methods=["POST"])
def returnresult():
    ylist=getmovie()
    plist=getpic()
    result_info={}
    yuce=json.loads(json.dumps(result_info))
    result1=[{'movie':ylist[0],'pic':plist[0]},{'movie':ylist[1],'pic':plist[1]},{'movie':ylist[2],'pic':plist[2]},{'movie':ylist[3],'pic':plist[3]},{'movie':ylist[4],'pic':plist[4]},{'movie':ylist[5],'pic':plist[5]}]
    yuce['result1']=result1
    result=json.dumps(yuce, ensure_ascii=False)
    return result
def getmovie():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getname=json_data.get('moviename')
    mysql = pymysql.connect ("localhost", "root","gly123", "movie")
    sql=('select * from plot')
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    movie=cursor.fetchall()
    pandas.DataFrame(movie).to_csv('movie_new.csv')
    movies=pandas.io.parsers.read_csv('movie_new.csv')
    tfidf=TfidfVectorizer()
    tfidf_matrix=tfidf.fit_transform(movies['type'])
    cosine_sim=linear_kernel(tfidf_matrix,tfidf_matrix)
    indices=pandas.Series(movies.index,index=movies['name']).drop_duplicates()
    def get_recommendation(title,consine_sim=cosine_sim):
        idx=indices[title]
        sim_scores=list(enumerate(cosine_sim[idx]))
        sim_scores=sorted(sim_scores,key=lambda x:x[1],reverse=True)
        sim_scores=sim_scores[1:11]
        movie_indices=[i[0]for i in sim_scores]
        return movies['name'].iloc[movie_indices]
    ylist=list(get_recommendation(getname))
    return ylist
def getpic():
    ylist=getmovie()
    plist={}
    pdlist={}
    mysql = pymysql.connect ("localhost", "root","gly123", "movie")
    sql1=('select img from plot where name="%s"'%ylist[0])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql1)
    pic1=cursor.fetchone()
    plist[0]=pic1.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[0],"wb")
    f.write(plist[0])
    f.close()
    pdlist[0]="PIC/%s.jpg"%ylist[0]
    sql2=('select img from plot where name="%s"'%ylist[1])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql2)
    pic2=cursor.fetchone()
    plist[1]=pic2.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[1],"wb")
    f.write(plist[1])
    f.close()
    pdlist[1]="PIC/%s.jpg"%ylist[1]
    sql3=('select img from plot where name="%s"'%ylist[2])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql3)
    pic3=cursor.fetchone()
    plist[2]=pic3.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[2],"wb")
    f.write(plist[2])
    f.close()
    pdlist[2]="PIC/%s.jpg"%ylist[2]
    sql4=('select img from plot where name="%s"'%ylist[3])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql4)
    pic4=cursor.fetchone()
    plist[3]=pic4.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[3],"wb")
    f.write(plist[3])
    f.close()
    pdlist[3]="PIC/%s.jpg"%ylist[3]
    sql5=('select img from plot where name="%s"'%ylist[4])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql5)
    pic5=cursor.fetchone()
    plist[4]=pic5.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[4],"wb")
    f.write(plist[4])
    f.close()
    pdlist[4]="PIC/%s.jpg"%ylist[4]
    sql6=('select img from plot where name="%s"'%ylist[5])
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql6)
    pic6=cursor.fetchone()
    plist[5]=pic6.get('img')
    f = open("F:/PIC/%s.jpg"%ylist[5],"wb")
    f.write(plist[5])
    f.close()
    pdlist[5]="PIC/%s.jpg"%ylist[5]
    return pdlist
#登录
@app.route('/login',methods=["POST"])
def getlogin():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('username')
    getsecret=json_data.get('mima')
    mysql = pymysql.connect ("localhost", "root","gly123", "movie")
    sql=('select user_password from user_if where user_name="%s"'%(getusername))
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    password=cursor.fetchone()
    a=password.get('user_password')
    if a==getsecret:
        message='well'
    else:
        message='wrong'
    result_info={}
    login=json.loads(json.dumps(result_info))
    login1=[{'login':message}]
    login['login1']=login1
    result=json.dumps(login, ensure_ascii=False)
    return result
#注册
@app.route('/register',methods=["POST"])
def userregister():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('username')
    getpassword1=json_data.get('email')
    getpassword2=json_data.get('mima')
    mysql = pymysql.connect ("localhost", "root","gly123", "movie")
    if getpassword1==getpassword2:
        sql1=('select count(*) from user_if')
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        user_id=cursor.fetchone()
        userid=user_id.get('count(*)')+1
        sql2=('insert into user_if (user_id,user_name,user_password) values (%d,"%s","%s")'%(userid,getusername,getpassword1))
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql2)
        message='well'
    else:
        message='wrong'
    result_info={}
    register=json.loads(json.dumps(result_info))
    register1=[{'register':message}]
    register['register1']=register1
    result=json.dumps(register, ensure_ascii=False)
    return result
#剧情
@app.route('/plot',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from plot1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from plot1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#喜剧
@app.route('/comedy',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from comedy1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from comedy1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#动作
@app.route('/action',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from action1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from action1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#爱情
@app.route('/love',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from love1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from love1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#科幻
@app.route('/science',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from science1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from science1 where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#动画
@app.route('/animation',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from animation where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from animation where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
#悬疑
@app.route('/suspense',methods=['POST'])
def upgrade7():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('num')
    num=int(getnum)
    allname={}
    allpic={}
    allpiclist={}
    a=(10*(num-1))+1
    b=(10*num)+1
    for i in range(a,b):
        mysql = pymysql.connect ("localhost", "root","gly123", "movie")
        sql=('select name from suspense where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        name=cursor.fetchone()
        allname[i-1]=name.get('name')
        sql1=('select img from suspense where id=%s'%i)
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        pic=cursor.fetchone()
        allpic[i-1]=pic.get('img')
        f = open("F:/PIC/%s.jpg"%allname[i-1],"wb")
        f.write(allpic[i-1])
        f.close()
        allpiclist[i-1]="PIC/%s.jpg"%allname[i-1]
    result_info={}
    upg=json.loads(json.dumps(result_info))
    result1=[{'movie':allname[(num-1)*10],'pic':allpiclist[(num-1)*10]},{'movie':allname[(num-1)*10+1],'pic':allpiclist[(num-1)*10+1]},{'movie':allname[(num-1)*10+2],'pic':allpiclist[(num-1)*10+2]},{'movie':allname[(num-1)*10+3],'pic':allpiclist[(num-1)*10+3]},{'movie':allname[(num-1)*10+4],'pic':allpiclist[(num-1)*10+4]},{'movie':allname[(num-1)*10+5],'pic':allpiclist[(num-1)*10+5]},{'movie':allname[(num-1)*10+6],'pic':allpiclist[(num-1)*10+6]},{'movie':allname[(num-1)*10+7],'pic':allpiclist[(num-1)*10+7]},{'movie':allname[(num-1)*10+8],'pic':allpiclist[(num-1)*10+8]},{'movie':allname[(num-1)*10+9],'pic':allpiclist[(num-1)*10+9]}]
    upg['result1']=result1
    result=json.dumps(upg, ensure_ascii=False)
    return result
if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True, port=4200)